package com.ruoyi.cloudsyncuser.controller;

import com.ruoyi.cloudsyncuser.dto.UserRegistrationRequest;
import com.ruoyi.cloudsyncuser.service.UserService;
import com.ruoyi.cloudsyncuser.utils.BaseResult;
import com.ruoyi.cloudsyncuser.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*") // 允许的源
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/sendCode")
    public BaseResult sendCode(@RequestParam("phoneNumber") String phoneNumber) {
        return userService.sendCode(phoneNumber);
    }

    @RequestMapping("/regUser")
    public BaseResult regUser(@RequestBody UserRegistrationRequest requestUser, HttpServletRequest request) {
        String clientIp = IpUtil.getClientIp(request);
        return userService.registerUser(requestUser, clientIp);
    }

    @RequestMapping("/pwdLogin")
    public BaseResult loginUserByPwd(@RequestBody Map<String,String>params) {
        return userService.loginByPwd(params.get("username"),params.get("password"));
    }

    @RequestMapping("/telLogin")
    public BaseResult loginByTel(@RequestBody Map<String,String>params) {
        return userService.loginByTel(params.get("phoneNumber"),params.get("code"));
    }

    @RequestMapping("/verifyToken")
    public BaseResult verifyToken(@RequestBody Map<String,String>params) {
        return userService.verifyToken(params.get("token"));
    }
}
